'''
https://leetcode.cn/problems/sum-of-left-leaves/description/
'''
from typing import Optional


# Definition for a binary tree node.
class TreeNode:
    def __init__(self, val=0, left=None, right=None):
        self.val = val
        self.left = left
        self.right = right
class Solution:
    def sumOfLeftLeaves(self, root: Optional[TreeNode]) -> int:
        def f(root, isleft):
            if not root.left and not root.right:
                return root.val if isleft else 0
            res = 0
            if root.left:
                res += f(root.left, True)
            if root.right:
                res += f(root.right, False)
            return res
        return f(root, False)